package com.banciyuan.bcywebview.biz.setting;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.UserApi;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.utils.http.BcyPair;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by hy on 2015/6/11.
 */
public class ChangeIntroActivity extends BaseActivity implements View.OnClickListener {
    private String desc;
    private View tvCancle;
    private TextView tvSave;
    private TextView tvModifyTitle;
    private EditText etDesc;
    private RequestQueue mQueue;
    private Boolean CLICK_GLAG = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changeintro);
        initArgs();
        initUi();
        initAction();

    }

    @Override
    protected void initArgs() {
        mQueue = VolleyQueue.getRquest(this);
        Intent intent = getIntent();
        desc = intent.getStringExtra("desc");

    }

    @Override
    protected void initProgressbar() {
    }

    @Override
    protected void initUi() {
        RelativeLayout rlDesc = (RelativeLayout) findViewById(R.id.rl_desc);
        tvCancle = findViewById(R.id.tv_cancle);
        tvSave = (TextView) findViewById(R.id.tv_save);
        tvModifyTitle = (TextView) findViewById(R.id.tv_modify_title);
        rlDesc.setVisibility(View.VISIBLE);
        etDesc = (EditText) findViewById(R.id.et_desc);
        if (!TextUtils.isEmpty(desc)) {
            etDesc.setText(Html.fromHtml(desc));
        }
        if (TextUtils.isEmpty(desc)) {
            tvSave.setTextColor(this.getResources().getColor(R.color.font_color));
            CLICK_GLAG = false;
        }
    }

    @Override
    protected void initAction() {
        tvCancle.setOnClickListener(this);
        tvSave.setOnClickListener(this);
        etDesc.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                int length = editable.toString().length();
                if (length >= 100) {
                    tvSave.setTextColor(getResources().getColorStateList(R.color.font_color));
                    MyToast.show(ChangeIntroActivity.this, getString(R.string.self_intro_over100));
                    CLICK_GLAG = false;
                    return;
                }
                tvSave.setTextColor(getResources().getColorStateList(R.color.pink));
                CLICK_GLAG = true;
            }
        });
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onNetworkError(String result) {
    }

    @Override
    protected Context getContext() {
        return super.getContext();
    }

    @Override
    protected void onResume() {
        super.onResume();
        showSoftInput(etDesc);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void showSoftInput(final EditText et) {
        et.requestFocus();
        et.setFocusable(true);
        et.setFocusableInTouchMode(true);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(et, 0);
            }
        }, 200);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cancle:
                finish();
                break;
            case R.id.tv_save:
                if (CLICK_GLAG) {
                    changeDesc();
                }
                changeDesc();
                break;
        }
    }

    private void changeDesc() {
        List<BcyPair> paramsList = new ArrayList<BcyPair>();
        String url = HttpUtils.BASE_URL + UserApi.getModifyintro();
        String content = etDesc.getText().toString().trim();
        if (UserDataHelper.ifLogin(this))
            paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(this).getToken()));
        paramsList.add(new BcyPair("self_intro", content));
        final HashMap<String, String> params = HttpUtils.getData(paramsList);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                JSONObject result = null;
                try {
                    result = new JSONObject(s);
                    String st = result.getString(HttpUtils.UPLOAD_STATUS);
                    String data = result.getString(HttpUtils.UPLOAD_DATA);
                    if (st.equals("1")) {
                        Intent intent = new Intent();
                        intent.putExtra("intro", etDesc.getText().toString().trim());
                        ChangeIntroActivity.this.setResult(Activity.RESULT_OK, intent);
                        ChangeIntroActivity.this.finish();
                    } else {
                        MyToast.show(ChangeIntroActivity.this, data);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    MyToast.show(ChangeIntroActivity.this, getString(R.string.modify_wrong));
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                MyToast.show(ChangeIntroActivity.this, getString(R.string.network_error));
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return params;
            }

        };
        mQueue.add(stringRequest);
    }
}
